package util;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;

/**
 * @author Administrator
 * @date 2018-11-29 14:42
 */
public class ChartGenerate {
    /**
     * 一般流程:
     * 1. 创建绘制图标的数据集合
     * 2. 创建 JFreeChart 实例
     * 3. 自定义图标绘制属性(可选)
     * 4. 生成自定义属性图片
     * 普通柱状图
     */
    public static String baseBarChartTest(HttpSession session) throws IOException {
        // 创建主题样式
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        // 设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 20));
        // 设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
        // 设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15));
        // 应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(250, "深圳", "苹果");
        dataset.addValue(180, "深圳", "橘子");
        dataset.addValue(350, "深圳", "香蕉");
        dataset.addValue(450, "深圳", "橙子");
        dataset.addValue(550, "深圳", "柠檬");

        JFreeChart jFreeChart = ChartFactory.createBarChart("水果销售柱状图",
                "水果", "销售", dataset,
                PlotOrientation.VERTICAL, true, true, false);

        System.out.println("创建成功");
        return ServletUtilities.saveChartAsPNG(jFreeChart, 700, 500, session);
    }

}
